package de.hsmannheim.poolraumapp.fragment;

import java.util.List;
import java.util.Set;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import de.hsmannheim.poolraumapp.R;
import de.hsmannheim.poolraumapp.activity.DetailActivity;
import de.hsmannheim.poolraumapp.activity.NavigationActivity;
import de.hsmannheim.poolraumapp.model.RoomQuery;
import de.hsmannheim.poolraumapp.model.RoomQuery.Where;

/**
 * A fragment representing a single Item detail screen. This fragment is either
 * contained in a {@link NavigationActivity} in two-pane mode (on tablets) or a
 * {@link DetailActivity} on handsets.
 */
public class WhereFragment extends PoolraumFragment implements OnCheckedChangeListener {
	private static final String TAG = WhereFragment.class.getName();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_where, container, false);
		
		RoomQuery roomQuery = getRoomQuery();
		
		GridLayout buildingNames = (GridLayout)view.findViewById(R.id.where_building_names);
		for (String buildingName : getResources().getStringArray(R.array.where_building_names)) {
			CheckBox checkBox = new CheckBox(getActivity());
			checkBox.setText(buildingName);
			checkBox.setChecked(roomQuery.getBuildings().contains(buildingName));
			checkBox.setOnCheckedChangeListener(this);
			buildingNames.addView(checkBox);
		}
		
		setChecked(view, getId(roomQuery.getWhere()));
		
		((RadioButton)view.findViewById(R.id.where_anywhere)).setOnCheckedChangeListener(this);
		((RadioButton)view.findViewById(R.id.where_buildings)).setOnCheckedChangeListener(this);
		
		return view;
	}
	
	private int getId(Where where) {
		switch(where) {
			case ANYWHERE:
				return R.id.where_anywhere;
			case BUILDINGS:
				return R.id.where_buildings;
		}
		throw new RuntimeException();
	}

	private void setChecked(View view, int id) {
		Log.d(TAG, "Enabling and disabling where views");
		((RadioButton)view.findViewById(R.id.where_anywhere)).setChecked(id == R.id.where_anywhere);
		boolean buildingsEnabled = id == R.id.where_buildings;
		((RadioButton)view.findViewById(R.id.where_buildings)).setChecked(buildingsEnabled);
		ViewGroup buildingNames = (ViewGroup)view.findViewById(R.id.where_building_names);
		for (int i = 0 ; i < buildingNames.getChildCount(); i++) {
			buildingNames.getChildAt(i).setEnabled(buildingsEnabled);
		}
		Log.d(TAG, "Where views enabled and disabled");
	}

	@Override
	public void onCheckedChanged(CompoundButton button, boolean checked) {
		RoomQuery roomQuery = getRoomQuery();		
		switch (button.getId()) {
		case R.id.where_anywhere:
			if (checked) {
				roomQuery.setWhere(Where.ANYWHERE);
				setChecked(getView(), button.getId());
			}
			break;
		case R.id.where_buildings:
			if (checked) {
				roomQuery.setWhere(Where.BUILDINGS);
				setChecked(getView(), button.getId());
			}
			break;
		default:
			Set<String> buildings = roomQuery.getBuildings();
			if (checked) {
				buildings.add(button.getText().toString());
			}
			else {
				buildings.remove(button.getText().toString());
			}
			roomQuery.setBuildings(buildings);
		}
	}
}
